package com.shaohai.live;


import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.EditText;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.shaohai.live.entity.ApiPostTaskParams;
import com.shaohai.live.entity.jsonResult;
import com.shaohai.live.ui.FSQueryBarActivity;
import com.shaohai.live.utils.AppDefine;
import com.shaohai.live.utils.RuntimeCfg;
import com.shaohai.live.utils.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LoginActivity extends FSQueryBarActivity {
    @BindView(R.id.edit_pass) EditText edit_pass;
    @BindView(R.id.edit_phone) EditText edit_phone;
    @BindView(R.id.btnLogin) QMUIRoundButton btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initializeView();
    }

    @Override
    protected void initializeView() {
        initTopBar("讲师登录");
        btnLogin.setOnClickListener(view -> {
            if (StringUtils.isNullOrEmpty(edit_pass.getText().toString()) || StringUtils.isNullOrEmpty(edit_phone.getText().toString())) {
                showToast("请输入手机号码及登录密码");
                return;
            }
            refreshRequestData();
        });
    }

    @Override
    protected void refreshRequestData() {
        ApiPostTaskParams taskParams = new ApiPostTaskParams();
        taskParams.url = AppDefine.SERVEHOST.concat(AppDefine.TEACHER_LOGIN);
        taskParams.params.put("phone", edit_phone.getText().toString().trim());
        taskParams.params.put("pwd", edit_pass.getText().toString().trim());
        refreshRequestDataAsync(taskParams);
    }

    @Override
    protected void onRequestDataTaskSuccess(String responseString) {
        if (responseString == null || responseString.startsWith("ERROR")) {
            showToast("登录失败，".concat(responseString + "，").concat("请稍后重试"));
        }
        try {
            jsonResult<JsonObject> result = gson.fromJson(responseString, new TypeToken<jsonResult<JsonObject>>() {
            }.getType());
            if (result != null && result.respCode.equals("200")) {
                RuntimeCfg.token = result.data.get("token").getAsString();
                RuntimeCfg.userID = result.data.get("id").getAsInt();
                Intent intent = new Intent(context, HomeActivity.class);
                startActivity(intent);
                finish();
            } else if (result != null && !StringUtils.isNullOrEmpty(result.respMessage)) {
                showToast("登录失败，".concat(result.respMessage).concat("请稍后重试"));
            } else {
                showToast("登录失败，校验错误");
            }
        } catch (JsonParseException ex) {
            showToast("登录失败，校验错误");
        }
    }

}
